package com.apes.fn.scm.prepare.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 缴款单明细
 * <p>
 * mut
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PrepareDetail.CONST_TABLE_NAME)
public class PrepareDetail extends Model {
    public static final String CONST_TABLE_NAME = "mas_prepare_detail";


    @Id
    @Column(name = "id", length = 3)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "编号", padding = 5)
    @Comment("编码")
    private Integer id;  //编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;


    @Comment("收款日期")
    @Column(name = "payment_time")
    private Date paymentTime;

    @Comment("收款金额")
    @Column(name = "payment_amount")
    private double paymentAmount;

    @Column(name = "the_amount", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("本次缴款金额")
    private double theAmount = 0.0;

    @Column(name = "recovery_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("已缴款金额")
    private double recoveryAmount = 0;

    @Column(name = "freeze_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("冻缴款金额")
    private double freezeAmount = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "mas_to_prepare_id", referencedColumnName = "id")
    @Comment("代缴款单")
    private ToPrepare toPrepare;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("缴款单")
    @JoinColumn(name = "prepare_id", referencedColumnName = "id")
    private Prepare prepare;

    public double getPaymentAmount() {
        return toPrepare.getAmount();
    }
}
